﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace LarDosIdosos.NewAcessaDados
{
    public class AcessaDados
    {
        /// <summary>
        /// Conexão com o mysql
        /// </summary>
        private MySqlConnection connectionDB;

        /// <summary>
        /// Cria uma nova instancia de conexão com o mysql
        /// </summary>
        /// <param name="connectionString"></param>
        public AcessaDados(string connectionString)
        {
            try
            {
                connectionDB = new MySqlConnection(connectionString);
            }
            catch(Exception ex)
            {
                throw new Exception("Erro na conexão: " + connectionString, ex);
            }

        }

        /// <summary>
        /// Abre uma conexão com o mysql
        /// </summary>
        public void OpenConnection()
        {
            try
            {
                if (connectionDB.State == ConnectionState.Closed)
                {
                    connectionDB.Open();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("OpenConnection: ", ex);
            }

        }

        /// <summary>
        /// Executa uma reader
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public MySqlDataReader ExecuteDataReader(string sql)
        {
            try
            {
                var cmd = new MySqlCommand(sql, connectionDB);
                var dr = cmd.ExecuteReader();
                cmd.Dispose();
                return dr;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ExecuteDataReader: ", ex);
            }
        }

        /// <summary>
        /// Executa um nonquery
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql)
        {
            try
            {
                var cmd = new MySqlCommand(sql, connectionDB);
                int total = cmd.ExecuteNonQuery();
                cmd.Dispose();
                return total;
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ExecuteDataReader: ", ex);
            }
        }

        /// <summary>
        /// Executa um dispose para fechar a conexão com o mysql
        /// </summary>
        public void CloseConection()
        {
            Dispose();
        }

        /// <summary>
        /// Fecha conexão com o mysql
        /// </summary>
        private void Dispose()
        {
            if (connectionDB != null)
            {
                if (connectionDB.State != ConnectionState.Closed)
                {
                    connectionDB.Close();
                    connectionDB = null;
                }
            }
        }
    }
}
